/**
 * Created by IntelliJ IDEA.
 * User: p.popov
 * Date: 28.03.13
 * Time: 14:23
 * To change this template use File | Settings | File Templates.
 */
package slicesPanels {
import flash.geom.Point;

import SimpleImage;
import slicesPanels.ParametersValidator;

import starling.utils.deg2rad;

public class Slice9PanelPart extends CSprite {
    private var innerScaleX:Number = 1;
    private var innerScaleY:Number = 1;
    private var image:SimpleImage;
    private var innerRotation:Number;
    private var innerSprite:CSprite;
    private var type: Slice9PartType;
    public function Slice9PanelPart() {
        innerSprite = new CSprite();
        addChild(innerSprite);
    }


    public static function create(type:Slice9PartType, resIds:Array, atlasPath:String):Slice9PanelPart {
        var result:Slice9PanelPart = new Slice9PanelPart();
        result.type = type;
		
		var src:String = ParametersValidator.getFirstValidParameter(ParametersValidator.checkForNotNull, resIds);
		
		result.image = AtlasHolder.instance.createSimpleImage(src, atlasPath);
        result.innerSprite.addChild(result.image);
        var scalePoint:Point = type.getScale(resIds);
        result.innerScaleX = scalePoint.x;
        result.innerScaleY = scalePoint.y;
        result.innerRotation = type.getRotation(resIds);
        result.rotation = deg2rad(result.innerRotation);
        return result;
    }

    override public function setScale(scaleX:Number, scaleY:Number):void {
        if (axisesNotShifted()) {
            image.setScale(scaleX, scaleY);
        } else {
            image.setScale(scaleY, scaleX);
        }
        setImagePivot();
    }

    override public function set scaleX(scaleX:Number):void {
        if (axisesNotShifted()) {
            image.scaleX = scaleX;
        } else {
            image.scaleY = scaleX;
        }

        setImagePivot();
    }

    override public function set scaleY(scaleY:Number):void {
        if (axisesNotShifted()) {
            image.scaleY = scaleY;
        } else {
            image.scaleX = scaleY;
        }
        setImagePivot();
    }

    override public function set width(width:Number):void {
        if (axisesNotShifted()) {
            image.width = width;
        } else {
            image.height = width;
        }
        setImagePivot();
    }

    override public function set height(height:Number):void {
        if (axisesNotShifted()) {
            image.height = height;
        } else {
            image.width = height;
        }
        setImagePivot();
    }


    private function setImagePivot():void {
        innerSprite.scaleX = innerScaleX;
        innerSprite.scaleY = innerScaleY;
        innerSprite.pivotX = Math.max(-(innerScaleX *width), 0);
        innerSprite.pivotY = Math.max(-(innerScaleY * height), 0);
        setPivot();

    }

    private function setPivot():void {
        switch (innerRotation) {
            case -90:
                pivotX = height;
                break;
            case 90:
                pivotY = width;
                break;
           case 180:
                pivotX = width;
                pivotY = height;
                break;
            case -180:
                pivotX = width;
                pivotY = height;
        }
    }

    public function setLoadCallback(onLoadingComplete:Function):void {
        if (image) {
            image.setLoadCallback(onLoadingComplete);
        }
    }

    private function axisesNotShifted():Boolean {
        var result:Number = (innerRotation / 90) % 2;
        return result == 0;
    }


    override public function get pivotX():Number {
        return image.pivotX + innerSprite.pivotX;
    }


    override public function get pivotY():Number {
        return image.pivotY + innerSprite.pivotY;
    }

	override public function setChildrenColor(color:int):void {
		image.color = color;
	}
}
}
